Added new classmethod xstransact.ListRecursive, and use this inside
authoremellor@ewan <emellor@ewan>
Wed, 28 Sep 2005 14:06:48 +0000 (15:06 +0100)
committeremellor@ewan <emellor@ewan>
Wed, 28 Sep 2005 14:06:48 +0000 (15:06 +0100)
DevController to tidy up the code there.  Change the semantics of
xstransact.list and List to return the empty list rather than None if there
are no entries or the directory is not present, as this is easier to handle
for the client.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/server/DevController.py
tools/python/xen/xend/xenstore/xstransact.py

index 5729e3e2b80c1cbfb2e633bb260b5dd7f6718713..0130075ac359ab9bdaa68487cacb3376fdf58856 100644 (file)
@@ -85,16 +85,7 @@ class DevController:
         """@return an s-expression describing all the devices of this
         controller's device-class.
         """
-        path = self.frontendRoot()
-        while True:
-            t = xstransact(path)
-            try:
-                listing = t.list_recursive()
-                if t.commit():
-                    return listing
-            except:
-                t.abort()
-                raise
+        return xstransact.ListRecursive(self.frontendRoot())
 
 
     def sxpr(self, devid):
index 503136ad76137550cee9bbb9145d92ff3ea1a344..8554b2ad8c5e7cc690e8f71627f56210c6445ac2 100644 (file)
@@ -115,12 +115,16 @@ class xstransact:
 
     def list(self, *args):
         """If no arguments are given, list this transaction's path, returning
-        the entries therein, or None if no entries are found.  Otherwise,
-        treat each argument as a subpath to this transaction's path, and
-        return the cumulative listing of each of those instead.
+        the entries therein, or the empty list if no entries are found.
+        Otherwise, treat each argument as a subpath to this transaction's
+        path, and return the cumulative listing of each of those instead.
         """
         if len(args) == 0:
-            return xshandle().ls(self.path)
+            ret = xshandle().ls(self.path)
+            if ret is None:
+                return []
+            else:
+                return ret
         else:
             ret = []
             for key in args:
@@ -141,8 +145,15 @@ class xstransact:
 
 
     def list_recursive(self, *args):
+        """If no arguments are given, list this transaction's path, returning
+        the entries therein, or the empty list if no entries are found.
+        Otherwise, treat each argument as a subpath to this transaction's
+        path, and return the cumulative listing of each of those instead.
+        """
         if len(args) == 0:
             args = self.list()
+            if args is None or len(args) == 0:
+                return []
 
         return self.list_recursive_(self.path, args)
 
@@ -248,11 +259,11 @@ class xstransact:
     Remove = classmethod(Remove)
 
     def List(cls, path, *args):
-        """If no arguments are given (path), list its contents, returning the
-        entries therein, or None if no entries are found.  Otherwise, treat
-        each further argument as a subpath to the given path, and return the
-        cumulative listing of each of those instead.  This operation is
-        performed inside a transaction.
+        """If only one argument is given (path), list its contents, returning
+        the entries therein, or the empty list if no entries are found.
+        Otherwise, treat each further argument as a subpath to the given path,
+        and return the cumulative listing of each of those instead.  This
+        operation is performed inside a transaction.
         """
         while True:
             t = cls(path)
@@ -266,6 +277,25 @@ class xstransact:
 
     List = classmethod(List)
 
+    def ListRecursive(cls, path, *args):
+        """If only one argument is given (path), list its contents
+        recursively, returning the entries therein, or the empty list if no
+        entries are found.  Otherwise, treat each further argument as a
+        subpath to the given path, and return the cumulative listing of each
+        of those instead.  This operation is performed inside a transaction.
+        """
+        while True:
+            t = cls(path)
+            try:
+                v = t.list_recursive(*args)
+                if t.commit():
+                    return v
+            except:
+                t.abort()
+                raise
+
+    ListRecursive = classmethod(ListRecursive)
+
     def Gather(cls, path, *args):
         while True:
             t = cls(path)